package com.enjoy.photoview;

import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.util.TypedValue;

public class Utils {

    private static final String TAG = Utils.class.getSimpleName();

    public static float dpToPixel(float dp) {
        return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp,
                Resources.getSystem().getDisplayMetrics());
    }

    public static Bitmap getPhoto(Resources res, int width) {
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, R.drawable.photo, options);
        options.inJustDecodeBounds = false;
        //当inDensity和inTargetDensity不匹配的时候会进行缩放，
        // 在允许缩放inScaled=true时，一张图片的缩放比例scale=inTargetDensity/inDensity
        options.inDensity = options.outWidth;
        options.inTargetDensity = width;
        //options.inScaled = false;
        Log.d(TAG, options.outWidth + ":" + options.outHeight + ":" + res.getDisplayMetrics());
        return BitmapFactory.decodeResource(res, R.drawable.photo, options);
    }
}
